package com.hardson.toolkit.swing.table;

import com.hardson.toolkit.swing.WrapLabel;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.awt.print.PrinterException;
import java.util.ArrayList;
import java.util.List;

public class TestMain {
    public static void main(final String[] args) {
        final JTable table = createTable();
        table.setRowHeight(35);
        table.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
        table.getTableHeader().setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
        table.setEnabled(false);
        table.setRowHeight(0, 70);
        table.setRowHeight(table.getRowCount() - 1, 120);
        //table.setGridColor(java.awt.Color.red);
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer() {
            private static final long serialVersionUID = 1L;

            @Override
            public Component getTableCellRendererComponent(final JTable table, final Object value,
                                                           final boolean isSelected, final boolean hasFocus,
                                                           final int row,
                                                           final int column) {

				/*if (row == 0) {
					if (column > 4 && column != 11) {
						this.setVerticalAlignment(SwingConstants.TOP);
					}
				}*/

                super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row,
                        column);
                this.setHorizontalAlignment(SwingConstants.CENTER);
                //this.setVerticalAlignment(SwingConstants.TOP);
                WrapLabel.wrap(table.getColumnModel().getColumn(column).getWidth(), this);
                this.setVerticalAlignment(SwingConstants.CENTER);

                return this;
            }
        };
        table.setDefaultRenderer(Object.class, renderer);
        //table.setFillsViewportHeight(true);
        table.getTableHeader().setFont(new Font("微软雅黑 Light", Font.PLAIN, 18));
        table.setFont(new Font("微软雅黑 Light", Font.PLAIN, 18));
        table.getTableHeader().setBackground(java.awt.Color.WHITE);
        JFrame frame = new JFrame("复杂JTable");
        frame.setSize(1500, 800);
        frame.setLayout(new GridBagLayout());
        frame.add(new JScrollPane(table),
                new GBC(0, 0).setWeight(100, 100).setFill(GridBagConstraints.BOTH));
        JButton printButton = new JButton("Print");
        printButton.addActionListener((e) -> printTb(table));

        frame.add(printButton, new GBC(0, 1));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    private static void printTb(final JTable tb) {
        try {
            tb.print();
        } catch (PrinterException e) {
            e.printStackTrace();
        }
    }

    public static JTable createTable() {
        TableContent tableContent = new TableContent();
        tableContent.append("<center>进<br/>馆<br/>档<br/>案<br/>数</center>", 1, 8);
        tableContent.append("年度");
        tableContent.append("保管期限");
        tableContent.append("项目数量");
        tableContent.append("案卷数量");
        tableContent.append("<center>卷内文件<br/>数量</center>");
        tableContent.append("<center>单件档案<br/>数量</center>");
        tableContent.append("<center>电子文件<br/>数量</center>");
        tableContent.append("<center>存储量<br/>(MB)</center>");
        tableContent.append("<center>冲印照片<br/>数量</center>");
        tableContent.append("<center>时长<br/>（时分秒）</center>");
        tableContent.append("档案页数");
        tableContent.append("<center>年度合计<br/>（卷）</center>");
        tableContent.append("<center>年度合计<br/>（卷内/件）</center>");
        tableContent.println();
        tableContent.append("2006", 1, 3);
        tableContent.append("1");
        tableContent.append("1");
        tableContent.append("1");
        tableContent.append("1");
        tableContent.println();
        tableContent.append("");
        tableContent.println();
        tableContent.append("");
        tableContent.println();
        tableContent.append("2007", 1, 3);
        tableContent.println();
        tableContent.append("");
        tableContent.println();
        tableContent.append("");
        tableContent.println();
        tableContent.append("合计", 2, 1);
        tableContent.println();
        tableContent.append("备注", 1, 3);
        tableContent.append("", 13, 3);
        tableContent.println();

        List<GroupHeader> list = new ArrayList<GroupHeader>();
        list.add(new GroupHeader("长沙市地方志办公室", 1, 6));
        list.add(new GroupHeader("文书档案", 12, 13));
        //				list.add(new GroupHeader("BUG类型", 3, 7));
        //				list.add(new GroupHeader("阶段", 1, 2));
        Object columnHeaders[] = new Object[]{ //
                "移交单位", //
                GroupHeader.EMPTY_LABEL, //
                GroupHeader.EMPTY_LABEL, //
                GroupHeader.EMPTY_LABEL, //
                GroupHeader.EMPTY_LABEL, //
                GroupHeader.EMPTY_LABEL, //
                GroupHeader.EMPTY_LABEL, //
                "全宗号", //
                "242", //
                "分类号", //
                "A", //
                "类别名称", //
                GroupHeader.EMPTY_LABEL, //
                GroupHeader.EMPTY_LABEL};
        JTable tb = DTable.create(tableContent, columnHeaders, list);
        return tb;

    }

    public static JTable createTable1() {
        TableContent tableContent = new TableContent();
        tableContent.append("测试项目1", 1, 8);
        tableContent.append("测试", 1, 2);
        tableContent.append("数量");
        tableContent.println();
        tableContent.append("比率");
        tableContent.println();
        tableContent.append("编码", 1, 2);
        tableContent.append("数量");
        tableContent.append("跨多行多列", 3, 4);
        tableContent.println();
        tableContent.append("比率");
        tableContent.println();
        tableContent.append("设计", 1, 2);
        tableContent.append("数量");
        tableContent.println();
        tableContent.append("比率");
        tableContent.println();
        tableContent.append("需求", 1, 2);
        tableContent.append("数量");
        tableContent.println();
        tableContent.append("比率");
        tableContent.println();
        List<GroupHeader> list = new ArrayList<GroupHeader>();
        list.add(new GroupHeader("BUG类型", 3, 7));
        list.add(new GroupHeader("阶段", 1, 2));
        return DTable.create(tableContent, new Object[]{"被测试项目", GroupHeader.EMPTY_LABEL,
                GroupHeader.EMPTY_LABEL, "逻辑错误", "文本错误", "接口错误", "程序错误", "合计"}, list);

    }
}

/**
 * 该类来自于《Java核心技术》
 *
 * @author Administrator
 */
class GBC extends GridBagConstraints {
    private static final long serialVersionUID = 1L;

    /**
     * Constructs a GBC with a given gridx and gridy position and all other grid
     * bag constraint values set to the default.
     *
     * @param gridx the gridx position
     * @param gridy the gridy position
     */
    public GBC(final int gridx, final int gridy) {
        this.gridx = gridx;
        this.gridy = gridy;
    }

    /**
     * Constructs a GBC with given gridx, gridy, gridwidth, gridheight and all
     * other grid bag constraint values set to the default.
     *
     * @param gridx      the gridx position
     * @param gridy      the gridy position
     * @param gridwidth  the cell span in x-direction
     * @param gridheight the cell span in y-direction
     */
    public GBC(final int gridx, final int gridy, final int gridwidth, final int gridheight) {
        this.gridx = gridx;
        this.gridy = gridy;
        this.gridwidth = gridwidth;
        this.gridheight = gridheight;
    }

    /**
     * Sets the anchor.
     *
     * @param anchor the anchor value
     * @return this object for further modification
     */
    public GBC setAnchor(final int anchor) {
        this.anchor = anchor;
        return this;
    }

    /**
     * Sets the fill direction.
     *
     * @param fill the fill direction
     * @return this object for further modification
     */
    public GBC setFill(final int fill) {
        this.fill = fill;
        return this;
    }

    /**
     * Sets the cell weights.
     *
     * @param weightx the cell weight in x-direction
     * @param weighty the cell weight in y-direction
     * @return this object for further modification
     */
    public GBC setWeight(final double weightx, final double weighty) {
        //super.w
        this.weightx = weightx;
        this.weighty = weighty;
        return this;
    }

    /**
     * Sets the insets of this cell.
     *
     * @param distance the spacing to use in all directions
     * @return this object for further modification
     */
    public GBC setInsets(final int distance) {
        this.insets = new Insets(distance, distance, distance, distance);
        return this;
    }

    /**
     * Sets the insets of this cell.
     *
     * @param top    the spacing to use on top
     * @param left   the spacing to use to the left
     * @param bottom the spacing to use on the bottom
     * @param right  the spacing to use to the right
     * @return this object for further modification
     */
    public GBC setInsets(final int top, final int left, final int bottom, final int right) {
        this.insets = new Insets(top, left, bottom, right);
        return this;
    }

    /**
     * Sets the internal padding
     *
     * @param ipadx the internal padding in x-direction
     * @param ipady the internal padding in y-direction
     * @return this object for further modification
     */
    public GBC setIpad(final int ipadx, final int ipady) {
        this.ipadx = ipadx;
        this.ipady = ipady;
        return this;
    }
}
